#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Net.Mail;
using NUnit.Framework;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Collections.ObjectModel;
using Newtonsoft.Utilities.Reflection;

namespace Newtonsoft.Utilities.Tests.Reflection
{
  public class ReflectionUtilsTests : TestsBase
  {
    [Test]
    public void GetTypeNameWithAssessmblyName()
    {
      Assert.AreEqual("Newtonsoft.Utilities.Tests.Reflection.ReflectionUtilsTests, Newtonsoft.Utilities.Tests", ReflectionUtils.GetNameAndAssessmblyName(typeof(ReflectionUtilsTests)));
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: t")]
    public void GetTypeNameWithAssessmblyNameTypeNull()
    {
      ReflectionUtils.GetNameAndAssessmblyName(null);
    }

    #region FindMemberTestClasses
    public class BaseClass
    {
      public string PublicBaseField;
      private string _privateBaseField;

      public string PublicBaseProperty
      {
        get { return _privateBaseField; }
        set { _privateBaseField = value; }
      }
    }

    public class InheritedClass : BaseClass
    {
      public string PublicInheritedField;
      private string _privateInheritedField;

      public string PublicInheritedProperty
      {
        get { return _privateInheritedField; }
        set { _privateInheritedField = value; }
      }
    }
    #endregion

    [Test]
    public void GetFields()
    {
      List<FieldInfo> fields;

      fields = ReflectionUtils.GetFields(typeof(InheritedClass), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
      Assert.AreEqual(4, fields.Count);
      CollectionAssert.AllItemsAreUnique(fields);

      fields = ReflectionUtils.GetFields(typeof(InheritedClass));
      Assert.AreEqual(4, fields.Count);
      CollectionAssert.AllItemsAreUnique(fields);

      fields = ReflectionUtils.GetFields(typeof(InheritedClass), BindingFlags.NonPublic | BindingFlags.Instance);
      Assert.AreEqual(2, fields.Count);
      CollectionAssert.AllItemsAreUnique(fields);

      fields = ReflectionUtils.GetFields(typeof(InheritedClass), BindingFlags.Public | BindingFlags.Instance);
      Assert.AreEqual(2, fields.Count);
      CollectionAssert.AllItemsAreUnique(fields);
    }

    [Test]
    public void FindMembers()
    {
      List<MemberInfo> members;

      members = ReflectionUtils.FindMembers(typeof(InheritedClass), MemberTypes.Field, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, null);
      Assert.AreEqual(4, members.Count);
      CollectionAssert.AllItemsAreUnique(members);

      members = ReflectionUtils.FindMembers(typeof(InheritedClass), MemberTypes.All, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, null);
      Assert.AreEqual(17, members.Count);
      CollectionAssert.AllItemsAreUnique(members);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: targetType")]
    public void FindMembersNullTarget()
    {
      ReflectionUtils.FindMembers(null, MemberTypes.All, BindingFlags.Default, null, null);
    }
  }
}